package service.impl;

import dao.GoodsDao;
import dao.NoteBookDao;
import dao.PhoneDao;
import dao.TvDao;
import dao.impl.GoodsDaoImpl;
import dao.impl.NoteBookDaoImpl;
import dao.impl.PhoneDaoImpl;
import dao.impl.TvDaoImpl;
import model.Goods;
import model.NoteBook;
import model.Phone;
import service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private PhoneDao phoneDao = new PhoneDaoImpl();
    private NoteBookDao noteBookDao = new NoteBookDaoImpl();
    private TvDao tvDao = new TvDaoImpl();

    @Override
    public Integer findLastGoods() {
        return goodsDao.findLastGoods();
    }

    @Override
    public boolean addPhone(Phone phone) {
        return phoneDao.add(phone);
    }

    @Override
    public boolean addGoods(Goods goods) {
        return goodsDao.add(goods);
    }

    @Override
    public List<Goods> findAll() {
        return goodsDao.findAll();
    }

    @Override
    public boolean delGoods(Integer id) {
        Goods goods = goodsDao.findByPK(id);
        System.out.println(goods.getCategory().getId());

        if (goods.getCategory().getId() ==1) {
            phoneDao.delByGoodsId(id);
        }else if (goods.getCategory().getId()==2){
            tvDao.delByGoodsId(id);
        }else if(goods.getCategory().getId()==3){
            noteBookDao.delByGoodsId(id);
        }
        return goodsDao.delByPK(id);
    }

    @Override
    public List<Goods> searchByName(String keyword) {
        return goodsDao.searchByName(keyword);
    }

    @Override
    public List<Goods> search(String keyword, Integer category, Integer state) {
        return goodsDao.search(keyword, category, state);
    }

    @Override
    public Goods findById(Integer id) {
        return goodsDao.findByPK(id);
    }


}
